package cn.tedu.staff_user.management.service.impl;

import cn.tedu.common.enumerator.ServiceCode;
import cn.tedu.common.ex.ServiceException;
import cn.tedu.common.pojo.po.StaffSearchPO;
import cn.tedu.staff_user.management.dao.persist.mapper.StaffMapper;
import cn.tedu.staff_user.management.dao.persist.repository.IStaffRepository;
import cn.tedu.staff_user.management.dao.search.StaffElasticsearchRepository;
import cn.tedu.staff_user.management.pojo.entity.Staff;
import cn.tedu.staff_user.management.pojo.param.StaffAddNewParam;
import cn.tedu.staff_user.management.pojo.param.StaffUpdateParam;
import cn.tedu.staff_user.management.pojo.vo.StaffListItemVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import cn.tedu.staff_user.management.service.IStaffService;

import javax.naming.Name;
import java.util.List;

import static cn.tedu.common.consts.data.CommonConsts.DISPLAY_STATE_OFF;
import static cn.tedu.common.consts.data.CommonConsts.DISPLAY_STATE_ON;

@Slf4j
@Service
public class StaffServiceImpl implements IStaffService {

    @Autowired
    private IStaffRepository staffRepository;

    @Override
    public List<StaffListItemVO> list() {
        return staffRepository.list();
    }

    @Autowired
    PasswordEncoder encoder;

    @Override
    public void addNew(StaffAddNewParam staffAddNewParam) {

        String username = staffAddNewParam.getUsername();
        log.error(username);
        int count = staffRepository.countName(username);

        log.error(count + "**********");
        if (count > 0) {
            String message = "添加员工失败，用户名被占用";
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }


        Staff staff = new Staff();
        BeanUtils.copyProperties(staffAddNewParam, staff);
        staff.setPassword(encoder.encode(staffAddNewParam.getPassword()));

        int i = staffRepository.addNew(staff);
        if (i != 1) {
            String message = "添加员工失败，服务器忙，请稍后再试";
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
    }

    @Override
    public void deleteById(Long id) {
        int i = staffRepository.deleteById(id);
        if (i != 1) {
            String message = "删除员工失败，服务器忙，请稍后再试";
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
    }

    @Override
    public void updateById(StaffUpdateParam staffUpdateParam) {
        Staff staff = new Staff();
        BeanUtils.copyProperties(staffUpdateParam, staff);
        int i = staffRepository.update(staff);
        if (i < 1) {
            String message = "更新员工失败，服务器忙，请稍后再试";
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }

    @Override
    public void setEnable(Long id) {
        log.debug("开始处理【优秀员工】的业务，参数：{}", id);
        updateDisplayById(id, DISPLAY_STATE_ON);
    }

    @Override
    public void disable(Long id) {
        log.debug("开始处理【非优秀员工】的业务，参数：{}", id);
        updateDisplayById(id, DISPLAY_STATE_OFF);
    }

    /**
     * 根据查看优秀员工接口
     */

    @Override
    public List<StaffListItemVO> selectList() {
        return staffRepository.selectList();
    }

    @Autowired
    private StaffElasticsearchRepository staffElasticsearchRepository;

    @Override
    public List<StaffSearchPO> listSearch(String username) {
        staffElasticsearchRepository.deleteAll();
        List<StaffSearchPO> list = staffRepository.listSearch();
        staffElasticsearchRepository.saveAll(list);
        return staffElasticsearchRepository.findByUsername(username);
    }


    private void updateDisplayById(Long id, Integer isDisplay) {

        Staff staff = new Staff();
        staff.setId(id);
        staff.setSuperStaff(isDisplay);
        int rows = staffRepository.updateById(staff);
        if (rows != 1) {
            String message = "将优秀的状态修改为【" + isDisplay + "】失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }
}

